package com.histone.grab_ticket.web;

import com.alibaba.fastjson.JSON;
import com.histone.grab_ticket.entity.ResultBean;
import com.histone.grab_ticket.entity.TicketEntity;
import com.histone.grab_ticket.service.TicketService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;

    @PostMapping()
    public ResultBean saveTicket(@RequestBody TicketEntity ticket) {
        ticket = ticketService.save(ticket);
        return new ResultBean(ticket);
    }

    @GetMapping
    public ResultBean getTickets(String contact, @RequestAttribute Claims claims) {
        System.out.println(JSON.toJSONString(claims));
        List<TicketEntity> tickets = ticketService.getTicektByContact(contact);
        return new ResultBean(tickets);
    }

    @GetMapping("{id}")
    public ResultBean getTicketById(@PathVariable String id){
        return new ResultBean(ticketService.getTicketById(id));
    }

    /**
     * 抢单
     */
    @PostMapping("grab")
    public ResultBean grabTicket(String ticketid, @RequestAttribute String userid){
        System.out.println(userid);
        return null;
    }
}
